package com.jade.generator.utils;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.jade.core.db.entity.BaseEntity;
import com.jade.core.web.controller.BaseController;
import com.jade.generator.entity.request.GeneratorReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.Serializable;

/**
 *  代码自定生成器（默认取主库生成）
 * @author Arnold
 * @version V1.0
 * @date 2021/3/17
 **/
@Slf4j
@Component
public class AutoGeneratorUtils implements Serializable {

    /**
     *  代码生成可参考：https://github.com/baomidou/generator
     * @param generatorReq
     * @return void
     * @author Arnold
     * @date 2022/5/19
     **/
    public void generator(GeneratorReq generatorReq) {
        FastAutoGenerator.create(new DataSourceConfig.Builder(generatorReq.getUrl(), generatorReq.getUserName(), generatorReq.getPassword())
                .typeConvert(new MySqlTypeConvert()).keyWordsHandler(new MySqlKeyWordsHandler()))
                // 全局配置
                .globalConfig(builder -> builder.fileOverride()
                        .outputDir(generatorReq.getOutputDir())
                        .author(generatorReq.getAuthor())
                        .dateType(DateType.TIME_PACK)
                        .commentDate(DatePattern.NORM_DATETIME_PATTERN))
                // 包配置
                .packageConfig(builder -> builder.parent(generatorReq.getPackageName())
                        .entity("entity")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("mapper")
                        .xml("mapper.xml")
                )
                // 策略配置
                .strategyConfig(builder -> builder.addTablePrefix(generatorReq.getTablePrefix())
                        .addInclude(generatorReq.getTableName())
                        .enableSkipView()
                        .addTablePrefix(generatorReq.getTablePrefix())
                        .controllerBuilder()
                        .superClass(BaseController.class)
                        .enableRestStyle()
                        .entityBuilder()
                        .superClass(BaseEntity.class)
                        .enableColumnConstant()
                        .enableChainModel()
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .addSuperEntityColumns(BaseEntity.ID, BaseEntity.CREATE_BY, BaseEntity.CREATE_TIME, BaseEntity.UPDATE_BY, BaseEntity.UPDATE_TIME, BaseEntity.DELETED)
                        .mapperBuilder()
                        .enableBaseResultMap()
                )
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }

}